// pages/login/login.js
const app = getApp();
let tool = require('../../utils/tool.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
    useInfor: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getSetting({
      success(res) {
        console.log("autolog界面---scope.userInfo="+res.authSetting['scope.userInfo'])
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              that.setUserInfo(res);
            }
          })
        }
      }
    });
    wx.login({
      success: function (res) {
        if (res.code) {
          app.globalData.code = res.code;
          let paramArr = {
            code: res.code,
          }
          let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
          let sign = rs.sign;
          let apiUrl = app.globalData.apiURL + "user/getopenid?sign=" + sign;
          let takesPro = new Promise(
            function (resolve, reject) {
              wx.request({
                url: apiUrl,
                header: rs.header,
                data: rs.bodyParam,
                method: 'post',
                success(res) {
                  resolve(res);
                }
              })
            }
          );
          takesPro.then(function (res) {
            if (res.data.code == 1) {//用openid登录
              let paramArr = {
                openid: res.data.data,
              }
              let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
              let sign = rs.sign;
              let apiUrl = app.globalData.apiURL + "user/loginbyopenid?sign=" + sign;
              let takesPro = new Promise(
                function (resolve, reject) {
                  wx.request({
                    url: apiUrl,
                    header: rs.header,
                    data: rs.bodyParam,
                    method: 'post',
                    success(res) {
                      if (res.data.code == 1) {//登录成功设置全局参数 跳转首页
                        app.globalData.studentInfo = res.data.data;
                        wx.switchTab({
                          url: '../index/index'
                        })
                      }else{
                        wx.redirectTo({
                          url: '../login/login',
                        })
                      }
                    }
                  })
                }
              );
            }else{
              wx.redirectTo({
                url: '../login/login',
              })
            }
          })
        } else {
          wx.redirectTo({
            url: '../login/login',
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setUserInfo: function (res) {
    this.setData({
      useInfor: res.userInfo
    })
    app.globalData.userInfo = this.data.useInfor;
  }
})